package com.zc.msmservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.zc.msmservice.service.MsmService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class MemServiceImpl implements MsmService {
	@Override
	public boolean send(Map<String, String> map, String phone) {
		if (StringUtils.isEmpty(phone)) {
			return false;
		}
		DefaultProfile profile =
				DefaultProfile.getProfile("default", "LTAI5tAnoZCcZWR8YWy1FPss", "PrehMZLDUJVqbLRtgQFaJDAuKSTWuY");
		IAcsClient client = new DefaultAcsClient(profile);

		CommonRequest request = new CommonRequest();
		//request.setProtocol(ProtocolType.HTTPS);
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");
		request.setAction("SendSms");

		request.putQueryParameter("PhoneNumbers", phone);
		request.putQueryParameter("SignName", "阿里云短信测试");
		request.putQueryParameter("TemplateCode", "SMS_154950909"); //模板code
		request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));

		try {
			CommonResponse response = client.getCommonResponse(request);
			System.out.println(response.getData());
			return response.getHttpResponse().isSuccess();
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
		return false;
	}
}
